--POUR CONTROLE


define erp='erp_mli2.';
define outils='outils_mli2.';

 /*CALL uvse_ExistsOrCreate('ART_REFEUN');*/

 DELETE FROM se_ART_REFEUN;
/*
select * from (
SELECT &outils.stragg(source) source,typunite_cod, refe_cod, refe_ind, unite_cod, enreg_id, numerunr_conv, denomunr_conv
FROM (
  SELECT 'uv' source ,g.* FROM se_ART_REFEUN g
  UNION ALL
  SELECT 'se' source ,g.* FROM &erp.ART_REFEUN g
  )

GROUP BY typunite_cod, refe_cod, refe_ind, unite_cod, enreg_id, numerunr_conv, denomunr_conv
HAVING Count(1)!=2 ) r
ORDER BY refe_cod,refe_ind,typunite_cod*/



 INSERT INTO se_ART_REFEUN
 (SELECT 'UNV' typunite_cod,
         r.refe_cod refe_cod,
         r.refe_ind refe_ind,
         Nvl(m.unite,'UN') unite_cod,
         0 enreg_id,
         1 numerunr_conv,
         1 denomunr_conv
    FROM se_art_refe r, uv_cmodeles m
    WHERE r.refe_cod || r.refe_ind= m.ID(+)
     );

 UPDATE se_ART_REFEUN tf SET enreg_id=(select enreg_id FROM &erp.ART_REFEUN f WHERE f.refe_cod=tf.refe_cod AND f.refe_ind=tf.refe_ind AND f.TYPUNITE_COD=tf.TYPUNITE_COD  ) ;
 UPDATE se_ART_REFEUN tf SET enreg_id=&outils.enreg_id.NEXTVAL WHERE enreg_id IS NULL OR enreg_id =0;




 commit;